package com.efast.cafe.portal.controller.common;


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ResponseBody;

import com.efast.cafe.framework.bean.ResultBean;
import com.efast.cafe.framework.controller.AbstractController;
import com.efast.cafe.framework.enums.ResultStatus;
import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.cafe.portal.bean.common.AttachmentFormBean;
import com.efast.cafe.portal.bean.common.tree.PortalTreeBean;
import com.efast.cafe.portal.bean.common.tree.PortalTreeMoveBean;
import com.efast.cafe.portal.entity.common.PortalAttachmentForm;
import com.efast.cafe.portal.entity.common.tree.PortalTree;
import com.efast.cafe.portal.entity.common.tree.TreeNode;
import com.efast.cafe.portal.service.common.IPortalTreeService;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * <p>Controller</p>
 * <p>Table: portal_tree - </p>
 *
 * @since 2018-04-25 11:04:19
 */
@Controller
public class PortalTreeController extends AbstractController{

	@Autowired
	private IPortalTreeService portalTreeService;
	
	

	@RequestMapping(value = "/crud/PortalTree/selectByPrimaryKey")
	public @ResponseBody ResultBean selectByPrimaryKey(@RequestBody PortalTree obj){
		ResultBean res = new ResultBean(true,null);
		try{
			PortalTree resObj = portalTreeService.selectByPrimaryKey(obj.getId());
			res.setObject(resObj);
		}catch(Exception e){
			e.printStackTrace();
			res.setSuccess(false);
			res.setErrorCode(ResultStatus.SYS_ERROR.getCode());
		}
		return res;
	}
	
	
	@RequestMapping(value = "/crud/PortalTree/deleteByPrimaryKey")
	public @ResponseBody ResultBean deleteByPrimaryKey(@RequestBody PortalTree obj){
		ResultBean res = new ResultBean(true,null);
		try{
			portalTreeService.deleteByPrimaryKey(obj.getId());
		}catch(Exception e){
			e.printStackTrace();
			res.setSuccess(false);
			res.setErrorCode(ResultStatus.SYS_ERROR.getCode());
		}
		return res;
	}
	
	
	@RequestMapping(value = "/crud/PortalTree/insert")
	public @ResponseBody ResultBean insert(@RequestBody PortalTree obj){
		ResultBean res = new ResultBean(true,null);
		try{
			obj.setOptCounter(0);
			obj.setCompanyCode(SpringWebUtils.getRequestCompany());
			obj.setLastUpdateTime(new Date());
			obj.setLastUpdateUser(SpringWebUtils.getRequestUser());
			obj.setCreateTime(new Date());
			obj.setCreateUser(SpringWebUtils.getRequestUser());
			portalTreeService.insert(obj);
		}catch(Exception e){
			e.printStackTrace();
			res.setSuccess(false);
			res.setErrorCode(ResultStatus.SYS_ERROR.getCode());
		}
		return res;
	}
	
	@RequestMapping(value = "/crud/PortalTree/updateByPrimaryKeySelective")
	public @ResponseBody ResultBean updateByPrimaryKeySelective(@RequestBody PortalTree obj){
		ResultBean res = new ResultBean(true,null);
		try{
			portalTreeService.updateByPrimaryKeySelective(obj);
		}catch(Exception e){
			e.printStackTrace();
			res.setSuccess(false);
			res.setErrorCode(ResultStatus.SYS_ERROR.getCode());
		}
		return res;
	}
	
	@RequestMapping(value = "/crud/PortalTree/updateByPrimaryKey")
	public @ResponseBody ResultBean updateByPrimaryKey(@RequestBody PortalTree obj){
		ResultBean res = new ResultBean(true,null);
		try{
			obj.setOptCounter(0);
			obj.setCompanyCode(SpringWebUtils.getRequestCompany());
			obj.setLastUpdateTime(new Date());
			obj.setLastUpdateUser(SpringWebUtils.getRequestUser());
			portalTreeService.updateByPrimaryKey(obj);
		}catch(Exception e){
			e.printStackTrace();
			res.setSuccess(false);
			res.setErrorCode(ResultStatus.SYS_ERROR.getCode());
		}
		return res;
	}
	
	@RequestMapping(value = "/crud/PortalTree/getTreeNodeType")
	public @ResponseBody ResultBean getTreeNodeType( @RequestBody(required=false) PortalTreeBean obj){
		ResultBean res = new ResultBean(true,null);
		try{
//			PortalTreeBean obj = new PortalTreeBean() ;
			//obj.setTreeTypeCode("tree001");
			//obj.setRootEntityKey("company");
			//obj.setId(1803);
			Map<String, Object> maps =  portalTreeService.getTreeNodeType(obj);
			res.setObject(maps);
		}catch(Exception e){
			e.printStackTrace();
			res.setSuccess(false);
			res.setErrorCode(ResultStatus.SYS_ERROR.getCode());
		}
		return res;
	}
	
	
	@RequestMapping(value = "/crud/PortalTree/getTreeList")
	public @ResponseBody ResultBean getTreeList(@RequestBody(required=false) PortalTreeBean obj){
		ResultBean res = new ResultBean(true,null);
		try{
//			PortalTreeBean obj1 = new PortalTreeBean() ;
//			obj1.setTreeTypeCode(obj.getTreeTypeCode());
//			obj1.setRootEntityKey("company");
//			obj1.setId(obj.getId());
//			obj.setEntityKey("company");
//			obj.setNodeTypeArr(new String[]{"company","position"});
			if(obj.getNodeTypeArr()!=null && obj.getNodeTypeArr().length>0){
				for (String nodeType : obj.getNodeTypeArr()) {
					if(StringUtils.isEmpty(obj.getNodeType())){
						obj.setNodeType(nodeType);
					}else{
						obj.setNodeType(obj.getNodeType()+ "," +nodeType);
					}
				} 
			}
			Map<String, Object> maps =  portalTreeService.queryTreeByCodeAndRootEntityKey(obj);
			res.setObject(maps);
		}catch(Exception e){
			e.printStackTrace();
			res.setSuccess(false);
			res.setErrorCode(ResultStatus.SYS_ERROR.getCode());
		}
		return res;
	}
	
	
	@RequestMapping(value = "/crud/{tableName}/addNode")
	public @ResponseBody ResultBean addNode(@PathVariable String tableName, @RequestBody Map<String, Object> map){
		ResultBean res = new ResultBean(true,null);
		try{
			Integer resId = null ;
			if (map.get("attachments") != null) {

				AttachmentFormBean attachments = new AttachmentFormBean();
				net.sf.json.JSONArray jry = net.sf.json.JSONArray.fromObject(map.get("attachments"));
				List<PortalAttachmentForm> list = net.sf.json.JSONArray.toList(jry, PortalAttachmentForm.class);
				attachments.setAttachments(list);
				resId = portalTreeService.addNode(tableName, map, attachments);
			} else {
				resId = portalTreeService.addNode(tableName, map, null);
			}
			TreeNode treeNode = new TreeNode() ;
			treeNode.setNodeId(resId);
			treeNode.setParentId((Integer)map.get("parentId"));
			String parentEntityKey =(String) map.get("parentEntitykey") ;
			map.put("entityKey", tableName);
			map.put("id", resId);
			Integer num  = (Integer)map.get("treeLevel") ;
			if(map.get("isSingle")!=null && (boolean)map.get("isSingle")){
				treeNode.setTreeLevel(num);
				map.put("treeLevel",num);
			}else{
				if(tableName.equals(parentEntityKey)){
					treeNode.setTreeLevel(num);
					map.put("treeLevel",num);
				}else{
					treeNode.setTreeLevel(num+1);
					map.put("treeLevel",num +1);
				}

			}
			treeNode.setBindData(map);
			List<TreeNode> treeNodes = new ArrayList<>() ;
			treeNodes.add(treeNode) ;
			Map<String, Object> maps = new HashMap<>() ;
			maps.put("treeJson", treeNodes);
			res.setObject(maps);
		}catch(Exception e){
			e.printStackTrace();
			res.setSuccess(false);
			res.setErrorCode(ResultStatus.SYS_ERROR.getCode());
		}
		return res;
	}
	
	
	@RequestMapping(value = "/crud/{tableName}/updateNode")
	public @ResponseBody ResultBean updateNode(@PathVariable String tableName, @RequestBody Map<String, Object> map){
		ResultBean res = new ResultBean(true,null);
		try{
			Integer resId = null ;
			if (map.get("attachments") != null) {

				AttachmentFormBean attachments = new AttachmentFormBean();
				net.sf.json.JSONArray jry = net.sf.json.JSONArray.fromObject(map.get("attachments"));
				List<PortalAttachmentForm> list = net.sf.json.JSONArray.toList(jry, PortalAttachmentForm.class);
				attachments.setAttachments(list);
				portalTreeService.updateNode(tableName, map, attachments);
			} else {
				portalTreeService.updateNode(tableName, map, null);
			}
			TreeNode treeNode = new TreeNode() ;
			treeNode.setNodeId((Integer)map.get("id"));
			treeNode.setParentId((Integer)map.get("parentId"));
			map.put("entityKey", tableName);
			Integer num  = (Integer)map.get("treeLevel") ;
			map.put("treeLevel",num);
			treeNode.setBindData(map); 
			treeNode.setTreeLevel(num);
			List<TreeNode> treeNodes = new ArrayList<>() ; 
			treeNodes.add(treeNode) ;
			Map<String, Object> maps = new HashMap<>() ;
			maps.put("treeJson", treeNodes);
			res.setObject(maps);
		}catch(Exception e){
			e.printStackTrace();
			res.setSuccess(false);
			res.setErrorCode(ResultStatus.SYS_ERROR.getCode());
		}
		return res;
	}
	
	@RequestMapping(value = "/crud/{tableName}/copyNode")
	public @ResponseBody ResultBean copyNode(@PathVariable String tableName, @RequestBody Map<String, Object> map){
		ResultBean res = new ResultBean(true,null);
		try{
			map.put("lastUpdateTime", null);
			Integer resId = null ;
			if (map.get("attachments") != null) {
				AttachmentFormBean attachments = new AttachmentFormBean();
				net.sf.json.JSONArray jry = net.sf.json.JSONArray.fromObject(map.get("attachments"));
				List<PortalAttachmentForm> list = net.sf.json.JSONArray.toList(jry, PortalAttachmentForm.class);
				attachments.setAttachments(list);
				
				resId = portalTreeService.addNode(tableName, map, attachments);
			} else {
				resId = portalTreeService.addNode(tableName, map, null);
			}
			TreeNode treeNode = new TreeNode() ;
			treeNode.setNodeId(resId);
			treeNode.setParentId((Integer)map.get("parentId"));
			map.put("entityKey", tableName);
			map.put("id", resId);
			Integer num  = (Integer)map.get("treeLevel") ;
			map.put("treeLevel",num);
			treeNode.setBindData(map); 
			treeNode.setTreeLevel(num);
			List<TreeNode> treeNodes = new ArrayList<>() ; 
			treeNodes.add(treeNode) ;
			Map<String, Object> maps = new HashMap<>() ;
			maps.put("treeJson", treeNodes);
			res.setObject(maps);
		}catch(Exception e){
			e.printStackTrace();
			res.setSuccess(false);
			res.setErrorCode(ResultStatus.SYS_ERROR.getCode());
		}
		return res;
	}
	
	
	@RequestMapping(value = "/crud/{tableName}/deleteNode")
	public @ResponseBody ResultBean deleteNode(@PathVariable String tableName, @RequestBody Map<String, Object> map) {
		ResultBean res = new ResultBean(true, "");
		try {
			portalTreeService.deleteNode(tableName, map);
		}catch(Exception e){
			e.printStackTrace();
			res.setSuccess(false);
			res.setErrorCode(ResultStatus.SYS_ERROR.getCode());
		}
		return res;
	}
	
	
	@RequestMapping(value = "/crud/PortalTree/getTreeAjax")
	public @ResponseBody ResultBean getTreeAjax( @RequestBody(required=false) PortalTreeBean obj){
		ResultBean res = new ResultBean(true,null);
		try{
//			 @RequestBody PortalTreeBean obj
//			PortalTreeBean obj = new PortalTreeBean() ;
//			obj.setTreeTypeCode("tree001");
//			obj.setRootEntityKey("company");
//			obj.setId(1803);
//			obj.setEntityKey("company");
//			obj.setNodeTypeArr(new String[]{"company","position"});
			if(obj.getNodeTypeArr()!=null && obj.getNodeTypeArr().length>0){
				for (String nodeType : obj.getNodeTypeArr()) {
					if(StringUtils.isEmpty(obj.getNodeType())){
						obj.setNodeType(nodeType);
					}else{
						obj.setNodeType(obj.getNodeType()+ "," +nodeType);
					}
				}
			}
			Map<String, Object> maps =  portalTreeService.queryTreeByCodeAndRootEntityKeyAjax(obj);
			res.setObject(maps);
		}catch(Exception e){
			e.printStackTrace();
			res.setSuccess(false);
			res.setErrorCode(ResultStatus.SYS_ERROR.getCode());
		}
		return res;
	}
	
	@RequestMapping(value = "/crud/PortalTree/moveTreeNode")
	public @ResponseBody ResultBean moveTreeNode( @RequestBody(required=false) PortalTreeMoveBean obj){
		ResultBean res = new ResultBean(true,null);
		try{
			portalTreeService.moveTree(obj);
		}catch(Exception e){
			e.printStackTrace();
			res.setSuccess(false);
			res.setErrorCode(ResultStatus.SYS_ERROR.getCode());
		}
		return res;
	}
	
	
}